/**
* Copyright (c) 2016-2100 yunclouds
*/
package com.yunqian.web;

import com.yunqian.redot.domain.Contents;
import com.yunqian.redot.service.ContentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Describe: 业务数据内容数据表（有顺序图或文字）控制器
 *
 * @author zsy
 * 2018-09-29 15:53:53
 */
 @RestController
 @RequestMapping("/api/contents")
public class ContentsController {
     @Autowired
     private ContentsService contentsService;

    /**
     * 获取分页
     *
     * @param params
     * @return
     */
    @PostMapping("/pagination")
    public Page<Contents> pagination(@RequestBody Map<String, Object> params) {
        return contentsService.pagination(params);
    }

     /**
     * 获取所有列表
     *
     * @return
     */
    @GetMapping("/all")
    public Map<String, Object> list() {
        List<Contents> contentsList = contentsService.findAll();
        Map<String, Object> map = new HashMap<>();
        map.put("contentsList", contentsList);
        return map;
    }

    /**
     * 添加
     *
     * @param contents 对象信息
     */
    @PostMapping
    public Contents save(@RequestBody @Valid Contents contents) {
        contentsService.save(contents);
        return contents;
    }

    /**
     * 局部更新数据
     *
     * @param id         待更新id
     * @param modifyData 待更新的数据
     * @return
     */
    @PutMapping("/{id}")
    public Contents update(@PathVariable("id") Long id, @RequestBody Map<String, Object> modifyData) {
        return contentsService.update(id, modifyData);
    }

    /**
     * 根据id获取详情
     *
     * @param id 要查询的id
     * @return
     */
    @GetMapping("/{id}")
    public Contents detail(@PathVariable("id") Long id) {
        return contentsService.findStrictOne(id);
    }

    /**
     * 删除数据
     *
     * @param ids 多个id使用逗号分隔
     */
    @DeleteMapping("/{ids}")
    public void delete(@PathVariable("ids") Long[] ids) {
        for (Long id : ids) {
            contentsService.delete(id);
        }
    }

}
